//
//  TSOrderTitleView.m
//  TSHomeFoodShop
//
//  Created by LYH on 2018/8/7.
//  Copyright © 2018年 Toocms. All rights reserved.
//

#import "TSOrderTitleView.h"
#import "TSOrderTitleColleCell.h"

@interface TSOrderTitleView()< UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>

@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,assign)NSInteger count;

@end

@implementation TSOrderTitleView

-(instancetype)initWithFrame:(CGRect)frame TitleArray:(NSArray *)titleArray CountOfLine:(NSInteger) countOfLine{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.titleArray = titleArray;
        self.count = countOfLine;
//        self.selectIndex = 0;
        [self createUI];
        // 分隔线
        CALayer * layer = [CALayer layer];
        layer.frame = Rect(0, self.height - 0.5, kScreenWidth, 0.5);
        layer.backgroundColor = RGBColor(171, 171, 171).CGColor;
        self.lineLayer = layer;
        [self.layer addSublayer:layer];
    }
    return self;
}

-(void)createUI{
    
    UICollectionViewFlowLayout *flowout = [UICollectionViewFlowLayout new];
    flowout.minimumLineSpacing = 0;
    flowout.minimumInteritemSpacing = 0;
    flowout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)collectionViewLayout:flowout];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    //    _collectionView.scrollEnabled = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.showsHorizontalScrollIndicator = NO;
    [_collectionView registerClass:[TSOrderTitleColleCell class] forCellWithReuseIdentifier:@"cell"];
    [self addSubview:_collectionView];
    [self.collectionView reloadData];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _titleArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    TSOrderTitleColleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
     cell.bottomLineView.hidden = _selectIndex == indexPath.row ? NO : YES;
    
    cell.titleLabel.text = _titleArray[indexPath.row];
    cell.titleLabel.textColor = _selectIndex == indexPath.row ? ThemeColor : [UIColor blackColor];
    
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return CGSizeMake(kScreenWidth / _count, self.height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    _selectIndex = indexPath.row;
    [self.collectionView reloadData];
    [self scrollToSelectIndex];
    
    [self.delegate didSelectItem:indexPath.row];
}


-(void)scrollToSelectIndex{
    
    if (self.count % 2 == 0) {
        
        if (_selectIndex > self.count / 2 && _selectIndex < self.titleArray.count - (self.count / 2)) {
            
            [UIView animateWithDuration:0.1 animations:^{
                [self.collectionView setContentOffset:CGPointMake(kScreenWidth / self->_count * ( self->_selectIndex - self.count / 2 ), 0 )];
            }];
        }
        
        if (_selectIndex <= self.count / 2 ) {
            
            [UIView animateWithDuration:0.1 animations:^{
                [self.collectionView setContentOffset:CGPointMake(0, 0 )];
            }];
        }
        
        if ( _selectIndex >= self.titleArray.count - (self.count / 2)) {
            
            [UIView animateWithDuration:0.1 animations:^{
                [self.collectionView setContentOffset:CGPointMake(kScreenWidth / self->_count * ( self.titleArray.count - self.count), 0 )];
            }];
        }
        
    }else{
        
        if (_selectIndex > self.count / 2 - 1 && _selectIndex < self.titleArray.count - (self.count / 2 + 1)) {
            
            [UIView animateWithDuration:0.1 animations:^{
                [self.collectionView setContentOffset:CGPointMake(kScreenWidth / self->_count * ( self->_selectIndex - self.count / 2), 0 )];
            }];
        }
        
        if (_selectIndex <= self.count / 2 - 1){
            
            [UIView animateWithDuration:0.1 animations:^{
                [self.collectionView setContentOffset:CGPointMake(0, 0 )];
            }];
            
        }
        
        
        if (_selectIndex >= self.titleArray.count - (self.count / 2 + 1)){
            
            [UIView animateWithDuration:0.1 animations:^{
                [self.collectionView setContentOffset:CGPointMake(kScreenWidth / self->_count * (self.titleArray.count - self.count), 0 )];
            }];
            
        }
    }
    
}

@end
